//
//  TCPRequest.m
//  zhongxiangdianhua
//
//  Created by longziyuan on 15-1-27.
//
//

#import "TCPRequest.h"
#import "JSONKit.h"
@implementation TCPRequest
@synthesize mydelegate;
/*************************TCP请求*****************************/

-(void)getTCPInfro:(NSMutableDictionary *)dic
{
   socket=[[AsyncSocket alloc] initWithDelegate:self];
    [socket connectToHost:@"ssl.mimicall.cn" onPort:8181 error:nil];
    
    
    
    
    
    
    
    
    
    
    
    
    [socket readDataWithTimeout:30.0 tag:1];
    
    
    
    NSString* jsonString=[NSString stringWithFormat:@"%@%@",[dic JSONString],@"\n"];
    NSLog(@"the json string:%@",jsonString);
    
    [socket writeData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30.0 tag:1];
    
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
    NSLog(@"did connect to host");
    
    
    
    
    
    
}





- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSLog(@"did read data");
    NSString* message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"message is: %@",message);
    
    if ([mydelegate respondsToSelector:@selector(returnBackSuccess:)]) {
        [mydelegate returnBackSuccess:message];
    }
    
     //[sock disconnect];
    // [socket disconnect];
    
    
    
    
}

-(void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    if ([mydelegate respondsToSelector:@selector(returnBackFail:)]) {
        [mydelegate returnBackFail:@"网络不通或者连接超时"];
    }
}

-(void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    //[sock disconnect];
}
-(void)dealloc
{
    [socket release];
    [super dealloc];
}
@end
